In [3]:
import re
import subprocess
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.graph_objs as go
import gpxpy
import random
from dateutil.parser import parse
from copy import deepcopy
from imageio import imread
from matplotlib.pyplot import imshow
from plotly.offline import init_notebook_mode, iplot

init_notebook_mode(connected=True)
%matplotlib inline


img = imread('basemap.png') / 255
plt.figure(figsize = (10,10))

# Find and show unique colors
img_array = img[:, :, :3].reshape((img.shape[0] * img.shape[1], 3))
colors = np.unique(img_array, axis=0)
n_colors = colors.shape[0]

def show_colors(colors):
    # colors_matrix = np.reshape(colors, [4, n_colors // 4, 3])
    imshow(np.reshape(colors, (1, -1, 3)), aspect='auto')
    plt.xticks([])
    plt.yticks([])
    plt.gcf().set_size_inches(10, 1)
    
    
# Create a custom colormap
color_to_value = {tuple(color[:3]): i / (n_colors - 1) for i, color in enumerate(colors)}
my_cmap_ply = [(value, 'rgb({}, {}, {})'.format(*color)) for color, value in color_to_value.items()]

# Map pixels to values
fun_find_value = lambda x: color_to_value[tuple(x[:3])]
values = np.apply_along_axis(fun_find_value, 2, np.flipud(img))

file = 'Lunch_Run.gpx'
gpx_file = open(file, 'r')
gpx = gpxpy.parse(gpx_file)

df = pd.DataFrame(columns=['lon', 'lat', 'alt', 'time'])

d1 = parse('2020-05-29T11:12:49Z')

for segment in gpx.tracks[0].segments: # all segments

    data = segment.points

    for point in data:
        timesec = (abs((point.time - d1).seconds))
        df = df.append({'lon': point.longitude, 'lat' : point.latitude, 'alt' : point.elevation, 'time' : timesec}, ignore_index=True)
 
traces = []

df.index = df['time']


trace = go.Scatter3d(
    
    x=df['lon'].values, y=df['lat'].values, z=df['time'].values,
    
    showlegend=False
)
traces.append(trace)  

yy = np.linspace(52.223, 52.242, img.shape[0])
xx = np.linspace(0.242, 0.284, img.shape[0])
zz = np.full(img.shape[:2], -90)


surf = go.Surface(
    x=xx, y=yy, z=zz,
    colorscale=my_cmap_ply,
    surfacecolor=values,
    showscale=False
)

layout = go.Layout(
    margin=dict(l=0,r=0,b=0,t=0),
    scene=go.layout.Scene(
        xaxis=go.layout.scene.XAxis(title='', showticklabels=False),
        yaxis=go.layout.scene.YAxis(title='', showticklabels=False),
        zaxis=go.layout.scene.ZAxis(title='Time (s)'),
        aspectratio=dict(x=1, y=1, z=1.3),
        camera=go.layout.scene.Camera(
            projection=go.layout.scene.camera.Projection(
                type='orthographic'
            )
        )
    )
)

fig = go.Figure(data=[surf] + traces, layout=layout)
iplot(fig, filename=f'adamgoesforarun.html')
<Figure size 720x720 with 0 Axes>
In [ ]: